﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryInsurancePolis: ADONetRepositoryBase<InsurancePolis>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(InsurancePolis entity)
        {
            var cmd = new SqlCommand("InsurancePolisInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Series", entity.Series));
            cmd.Parameters.Add(new SqlParameter("PolisNumber", entity.PolisNumber));
            cmd.Parameters.Add(new SqlParameter("Registration", entity.Registration));
            cmd.Parameters.Add(new SqlParameter("Expiration", entity.Expiration));
            cmd.Parameters.Add(new SqlParameter("InsuranceOrganizationId", entity.InsuranceOrganization.Id));
            logger.Trace("SqlCommand для записи данных Страхового полиса в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("InsurancePolisDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Страхового полиса id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(InsurancePolis entity)
        {
            var cmd = new SqlCommand("InsurancePolisUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Series", entity.Series));
            cmd.Parameters.Add(new SqlParameter("PolisNumber", entity.PolisNumber));
            cmd.Parameters.Add(new SqlParameter("Registration", entity.Registration));
            cmd.Parameters.Add(new SqlParameter("Expiration", entity.Expiration));
            cmd.Parameters.Add(new SqlParameter("InsuranceOrganizationId", entity.InsuranceOrganization.Id));
            logger.Trace("SqlCommand для обновления данных Страхового полиса id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetInsurancePolis", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Страхового полиса id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetInsurancePolisByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Страхового полиса {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override InsurancePolis GetEntity(SqlDataReader sqlDataReader)
        {
            InsurancePolis polis = new InsurancePolis();
            IRepository<InsuranceOrganization> repOrg = new ADONetRepositoryInsuranceOrganisation();
            polis.Id = (int)sqlDataReader["Id"];
            polis.Series = sqlDataReader["Series"].ToString();
            polis.PolisNumber = sqlDataReader["PolisNumber"].ToString();
            polis.Registration = (DateTime)sqlDataReader["Registration"];
            polis.Expiration = (DateTime)sqlDataReader["Expiration"];
            polis.InsuranceOrganization = repOrg.GetById((int)sqlDataReader["InsuranceOrganizationId"]);
            return polis;
        }
    }
}
